﻿#include "mainview.h"
#include <model/filemodel.h>
#include <qqmlcontext.h>
#include <util/fileutil.h>
#include <util/listutil.h>
#include <QString>
#include <QTextCodec>

MainView::MainView(QObject *parent) : QObject(parent) {};

QString MainView::openFile(QObject* mo) {
    FileModel* file = (FileModel*)mo;
    QString fullPath = file->fullPath();
    FileTreeService ser;
    QList<FileModel*> fileList = ser.openNode(AppUtil::fileTreeRoot, fullPath);

    // 清空
    AppUtil::dataList.clear();

    ListUtil::add(AppUtil::dataList, ListUtil::cast<FileModel*, QObject*>(fileList));

    QQmlContext* context = AppUtil::ENGINE->rootContext();
    QObject* obj = AppUtil::ENGINE->rootObjects()[0]->findChild<QObject*>("fileList");

    QVariant idx = obj->property("currentIndex");
    context->setContextProperty("myModel", QVariant::fromValue(AppUtil::dataList));
    obj->setProperty("currentIndex", idx);

    QString res = "";
    if(file->type() == "file"){
        res = FileUtil::readAll(file->fullPath());
    }

    return res;

}
